package hhm.android.main.history

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import hhm.android.base.adapter.HBaseAdapter
import hhm.android.main.R
import hhm.android.main.shoeSizeHistory.ShoeSizeHistoryListChildViewHolder
import siau.android.base.SBApplication
import siau.android.base.analyseDataBeforeSet
import siau.android.base.getSureDialog
import siau.android.http.model.ListHistoryChildModel
import java.text.SimpleDateFormat

class HistoryListInnerAdapter(var context: Context, list: ArrayList<ListHistoryChildModel>, var pageType: Int, var cla: String, var pageState: Int, var isGroupLast: Boolean, var itemCLick: View.OnClickListener, var deleteListener: View.OnClickListener) : HBaseAdapter<HistoryListChildViewHolder, ListHistoryChildModel>(list) {
    override fun bindData(vh: HistoryListChildViewHolder, b: ListHistoryChildModel, position: Int) {
        if (pageState == 1) {
            vh.select.visibility = View.GONE
            vh.sml.isSwipeEnable = true
        } else if (pageState == 2) {
            vh.select.visibility = View.VISIBLE
            vh.sml.isSwipeEnable = false
        }
        if (b.select) {
            vh.select.setImageResource(R.mipmap.list_history_select)
        } else {
            vh.select.setImageResource(R.mipmap.list_history_unselect)
        }
        val sdf = SimpleDateFormat("HH:mm")
        vh.time.text = sdf.format(b.recordTime)
        vh.weight.text = analyseDataBeforeSet(b.recordValue.toString(), pageType, cla)
        if (pageType == 1) {
            if (b.recordType == 1) {
                vh.iv.setImageResource(R.mipmap.weight_grey)
            } else {
                vh.iv.setImageResource(R.mipmap.input_by_hand_grey)
            }
        } else if (pageType == 2) {
            if (b.recordType == 1) {
                vh.iv.setImageResource(R.mipmap.height_grey)
            } else {
                vh.iv.setImageResource(R.mipmap.input_by_hand_grey)
            }
        }
        vh.itemView.setOnClickListener {
            it.tag = b
            itemCLick.onClick(it)
        }

        if (position == list.size - 1) {
            vh.line.visibility = View.GONE
            if (isGroupLast) {
                vh.line2.visibility = View.GONE
            } else {
                vh.line2.visibility = View.VISIBLE
            }
        } else {
            vh.line.visibility = View.VISIBLE
            vh.line2.visibility = View.GONE
        }

        vh.delete.setOnClickListener {
            if (SBApplication.userData.isCreator == 1) {
                vh.sml.quickClose()
                getSureDialog(
                    context, { i ->
                        i.tag = b
                        deleteListener.onClick(i)
                    }, content = context.getString(R.string.sure_delete_data), showTitle = false
                )
            } else {
                Toast.makeText(context, context.getString(R.string.you_cannot_delete_data), Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun bindView(parent: ViewGroup, viewType: Int): HistoryListChildViewHolder {
        return HistoryListChildViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_history_list_child, parent, false))
    }
}